package com.example.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.entity.*;
import com.example.mapp.*;
import com.example.util.SystemPointUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.example.ext.entity.ExtDailyInspection;
import com.example.service.DailyInspectionService;
import com.example.util.PromotionUtil;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import ch.qos.logback.classic.Logger;

/**
 * Created by simon on 2017/10/20.
 */
@Service
public class DailyInspectionServiceImpl implements DailyInspectionService {

	private final static Logger logger = (Logger) LoggerFactory.getLogger(DailyInspectionServiceImpl.class);

	@Autowired
	DailyInspectionMapp dailyInspectionMapp;

	@Autowired
	PromotionMapp promotionMapp;

	@Autowired
	PartyUserMapp partyUserMapp;

	@Autowired
	AfterDoMapp afterDoMapp;
	
	@Autowired
	private ElevatorMapp elevatorMapp;

	@Autowired
	private PointFlowMapp pointFlowMapp;



	@Override
	public Map<String, Object> queryListByElevatorId(ExtDailyInspection extDailyInspection) {
		List<ExtDailyInspection> list = null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Page<ExtDailyInspection> page = PageHelper.startPage(extDailyInspection.getCurrentPage(),
					extDailyInspection.getPageSize());
			list = dailyInspectionMapp.queryListByElevatorId(extDailyInspection);
			map.put("list", list);
			map.put("total", page.getTotal());

		} catch (Exception e) {
			logger.error(e.getMessage());
			return ResultUtil.returnError("查询失败" + e.getMessage(), null);
		}
		return ResultUtil.returnSuccess("查询成功", map);
	}

	/**
	 * 提交巡检记录
	 * @param dailyInspection
	 * @return
	 */
	@Override
	@Transactional
	public Map<String, Object> add(DailyInspection dailyInspection) {
		try {
			dailyInspectionMapp.add(dailyInspection);

			Elevator elevator = elevatorMapp.queryById(String.valueOf(dailyInspection.getElevatorId()));
			elevator.setLastInspectDate(dailyInspection.getCheckStartDt());
			elevatorMapp.update(elevator);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return ResultUtil.returnError("提交失败" + e.getMessage(), null);
		}
		try{
			AfterDo afterDo = new AfterDo();
			afterDo.setEventType("2");
			afterDo.setStatus("1");
			AfterDo afterDo1 = afterDoMapp.findByType(afterDo);
			return ResultUtil.returnSuccess("提交成功", afterDo1);
		}catch (Exception e){
			logger.error("查询巡检场景失败",e);
		}finally {
			if (dailyInspection != null) {
				// 获取巡检记录缓存
				PointDefendInfoCache infoCache = new PointDefendInfoCache();
				PointDefend defend = infoCache.getKeyByValue("inspection_record");

				// 获取当前用户，计算积分
				Long userId = dailyInspection.getCheckUserId();
				Map<String, Object> maps = pointFlowMapp.findUserInfoByUserId(userId);
				long mark = (long) maps.get("mark");
				int round = (int) (mark + defend.getMark());

				// 更新用户信息总积分
				String userIds = maps.get("userId").toString();
				int i1 = pointFlowMapp.updateMark(round, userIds);

				String userName = maps.get("userName").toString();
				String partyName = maps.get("partyName").toString();
				SystemPointUtil.insertFlow(pointFlowMapp, i1, defend, round, userName, partyName, "提交一份巡检记录");

				// 巡检加使用单位的积分
				PointDefend defendUnit = infoCache.getKeyByValue("inspection_record_units");
				// 通过用户获取单位系统管理员信息
				Map<String, Object> mapUnit = pointFlowMapp.getAdminInfoByUserId(userId);

				long markUnit = (long) mapUnit.get("mark");
				int roundUnit = (int) (markUnit + defendUnit.getMark());

				// 更新用户信息总积分
				String userIdUnit = mapUnit.get("userId").toString();
				int i2 = pointFlowMapp.updateMark(roundUnit, userIdUnit);

				String name = mapUnit.get("userName").toString();
				String pname = mapUnit.get("partyName").toString();
				SystemPointUtil.insertFlow(pointFlowMapp, i2, defendUnit, roundUnit, name, pname, "提交一份巡检记录加单位的积分");

			}
		}
		return ResultUtil.returnSuccess("提交成功", null);
	}
}
